import requests
from bs4 import BeautifulSoup


def get_web_text(url):
    try:
        response = requests.get(url, timeout=10)
        response.encoding = response.apparent_encoding  # 自动检测编码
        soup = BeautifulSoup(response.text, 'html.parser')

        # 移除不需要的元素
        for element in soup(['script', 'style', 'nav', 'footer', 'head', 'meta']):
            element.decompose()

        # 获取纯文本
        text = soup.get_text(separator='\n', strip=True)

        return text

    except requests.exceptions.RequestException as e:
        return str(e)


def get_web_html(url):
    try:
        response = requests.get(url, timeout=10)
        response.encoding = response.apparent_encoding  # 自动检测编码
        soup = BeautifulSoup(response.text, 'html.parser')

        # 移除不需要的元素
        for element in soup(['script', 'style', 'nav', 'footer', 'head', 'meta']):
            element.decompose()

        # 获取纯文本
        html = soup.prettify()

        return html

    except requests.exceptions.RequestException as e:
        return str(e)
